package com.chinaoly.baotou.common.utils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * @author xuyh
 * @date 2022/3/25
 */
public class FileUtil {

    public static void download(HttpServletResponse response, InputStream inputStream, String fileName) throws IOException {
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
        response.flushBuffer();
        int len = 0;
        byte[] buffer = new byte[1024];
        ServletOutputStream out = response.getOutputStream();
        while ((len = inputStream.read(buffer)) > 0) {
            //将缓冲区的数据输出到浏览器
            out.write(buffer, 0, len);
        }
        out.close();
        inputStream.close();
    }

}
